package chen.web.controller.forum;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;

import chen.web.MyContextProvider;
import chen.web.controller.ResourceNotFoundException;
import chen.web.favorite.Favorite;
import chen.web.favorite.FavoriteManager;
import chen.web.forum.PostAmountLevel;
import chen.web.forum.PostAmountLevel.Level;
import chen.web.forum.Reply;
import chen.web.forum.ReplyCondition;
import chen.web.forum.Subject;
import chen.web.forum.SubjectCondition;
import chen.web.forum.TopicInfo;
import chen.web.forum.TopicManager;
import chen.web.forum.ForumAccount;
import chen.web.forum.ForumAccountLoader;
import chen.web.message.Inbox;
import chen.web.message.Message;
import chen.web.message.MessageManager;
import chen.web.message.Outbox;
import chen.web.user.Account;
import chen.web.user.AccountManager;
import chen.web.user.NoPermissionException;
import chen.web.util.Pagination;



@RequestMapping(value="/member",method=RequestMethod.GET)
public class MemberCenter {
	
	private static final int Message_Count_Per_Page = 15;
	private static final int Favorite_Count_Per_Page = 15;
	private static final String Owner_Attribute_Name = "owner";
	
	private @Autowired AccountManager am;
	private @Autowired ForumAccountLoader fal;
	private @Autowired TopicManager tm;
	private @Autowired MessageManager mm;
	private @Autowired FavoriteManager fm;
	
	private ForumAccount getForumAccount(int id){
		ForumAccount account = fal.loadForumAccount(id);
		if(account == null){
			throw new ResourceNotFoundException();
		}
		return account;
	}
	
	private Account getAccount(int id){
		Account account = am.getAccount(id); 
		if(account == null){
			throw new ResourceNotFoundException();
		}
		return account;
	}
	
	@RequestMapping("/{id}")
	@ResponseStatus(HttpStatus.MOVED_PERMANENTLY) 
	public String center(@PathVariable int id){
		return "redirect:/member/" + id + "/profile";
	}
	
	@RequestMapping("/{id}/profile")
	public String profile(@PathVariable int id, Model model){
		ForumAccount owner = getForumAccount(id);
		if(owner == null){
			throw new ResourceNotFoundException();
		}
		model.addAttribute(Owner_Attribute_Name, owner);
		return "center/profile";
	}
	
	@RequestMapping("/{id}/upload-photo")
	public String uploadPhoto(@PathVariable int id, Model model){
		ForumAccount owner = getForumAccount(id);
		Account cua = MyContextProvider.getMyContext().getAccount();
/*		AccountPermissionConfig config = PermissionConfigLoader.load(AccountPermissionConfig.class);
		//权限判断
		if(config.isUpdateOwnAccount() && owner.equals(cua) || config.isUpdateAccount()){
			model.addAttribute("owner", owner);
			return "center/upload-photo";	
		}
		throw new NoPermissionException();*/
		model.addAttribute(Owner_Attribute_Name, owner);
		return "center/upload-photo";	
	}
	
	@RequestMapping("/{id}/edit-info")
	public String edit(@PathVariable int id, Model model){
		Account owner = getAccount(id);
/*		Account cua = MyContextProvider.getMyContext().getAccount();
		AccountPermissionConfig config = PermissionConfigLoader.load(AccountPermissionConfig.class);
		//权限判断
		if(config.isUpdateOwnAccount() && owner.equals(cua) || config.isUpdateAccount()){
			model.addAttribute("owner", owner);
			return "center/edit_info";
		}
		throw new NoPermissionException();*/
		model.addAttribute(Owner_Attribute_Name, owner);
		return "center/edit-info";
	}
	
	@RequestMapping("/{id}/role")
	public String role(@PathVariable int id, Model model){
		Account owner = getAccount(id);
		int subjectAmount = tm.totalSubject(new SubjectCondition().setAccount(owner));
		int replyAmount = tm.replyAmount(owner);
		Level level = PostAmountLevel.getLevel(subjectAmount + replyAmount);
		int atLeast = PostAmountLevel.topicCount(level);
		
		Map<String, Integer> levelMap = new LinkedHashMap<String, Integer>();
		Level[] values = Level.values();
		//级别名称和发帖量的对应关系
		for(Level l : values){
			levelMap.put(l.name(), PostAmountLevel.topicCount(l));
		}

		model.addAttribute("levelName", level.name());
		model.addAttribute("levelOrdinal", level.ordinal());
		model.addAttribute("atLeast", atLeast);
		model.addAttribute("levelMap", levelMap);
		model.addAttribute(Owner_Attribute_Name, owner);
		return "center/role";
	}
	
	@RequestMapping("/{id}/password")
	public String password(@PathVariable int id, Model model){
		Account owner = getAccount(id);
		Account cua = MyContextProvider.getMyContext().getAccount();
		//权限判断
		if(cua.equals(owner)){
			model.addAttribute("owner", owner);
			return "center/password";
		}
		throw new NoPermissionException();
	}
	
	@RequestMapping("/{id}/favorite")
	public String favorite(@PathVariable int id, Model model){
		Account owner = getAccount(id);
		int amount = fm.amount(owner);
		Pagination pagination = new Pagination(amount, Favorite_Count_Per_Page);
		List<Favorite> favoriteList = fm.listFavorite(owner, pagination.getBeginOffset(), pagination.getPageInfoCount());
		
		model.addAttribute(Owner_Attribute_Name, owner);
		model.addAttribute("pagination", pagination);
		model.addAttribute("favoriteList", favoriteList);
		return "center/favorite";
	}
	
	@RequestMapping("/{id}/favorite/topic")
	public String favoriteTopic(@PathVariable int id, Model model){
		Account owner = getAccount(id);
		int amount = fm.amount(owner, Subject.class);
		Pagination pagination = new Pagination(amount, Favorite_Count_Per_Page);
		List<Favorite> favoriteList = fm.listFavorite(owner, Subject.class, pagination.getBeginOffset(), pagination.getPageInfoCount());
		
		model.addAttribute(Owner_Attribute_Name, owner);
		model.addAttribute("pagination", pagination);
		model.addAttribute("favoriteList", favoriteList);
		model.addAttribute("type", "topic");
		return "center/favorite";
	}
	
	@RequestMapping("/{id}/favorite/reply")
	public String favoriteReply(@PathVariable int id, Model model){
		Account owner = getAccount(id);
		int amount = fm.amount(owner, Reply.class);
		Pagination pagination = new Pagination(amount, Favorite_Count_Per_Page);
		List<Favorite> favoriteList = fm.listFavorite(owner, Reply.class, pagination.getBeginOffset(), pagination.getPageInfoCount());
		
		model.addAttribute(Owner_Attribute_Name, owner);
		model.addAttribute("pagination", pagination);
		model.addAttribute("favoriteList", favoriteList);
		model.addAttribute("type", "reply");
		return "center/favorite";
	}
	
	@RequestMapping("/{id}/topic")
	public String listTopic(@PathVariable int id, Model model){
		Account owner = getAccount(id);
		SubjectCondition condition = new SubjectCondition().setAccount(owner);
		int amount = tm.totalSubject(condition);
		Pagination pagination = new Pagination(amount, 15);
		List<TopicInfo> topicInfoList = tm.getTopicInfoList(condition, pagination.getBeginOffset(), pagination.getPageInfoCount());
		
		model.addAttribute(Owner_Attribute_Name, owner);
		model.addAttribute("pagination", pagination);
		model.addAttribute("topicInfoList", topicInfoList);
		model.addAttribute("type", "topic");
		return "center/topic-list";
	}
	
	@RequestMapping("/{id}/replied-topic")
	public String listRepliedTopic(@PathVariable int id, Model model){
		Account owner = getAccount(id);
		SubjectCondition condition = new SubjectCondition().setAccount(owner);
		int amount = tm.totalSubject(condition);
		Pagination pagination = new Pagination(amount, 15);
		List<TopicInfo> topicInfoList = tm.getRepliedTopicInfoList(owner, pagination.getBeginOffset(), pagination.getPageInfoCount());
		
		model.addAttribute(Owner_Attribute_Name, owner);
		model.addAttribute("pagination", pagination);
		model.addAttribute("topicInfoList", topicInfoList);
		model.addAttribute("type", "replied");
		return "center/topic-list";
	}
	
	@RequestMapping("/{id}/reply")
	public String listReply(@PathVariable int id, Model model){
		ForumAccount owner = getForumAccount(id);
		Pagination pagination = new Pagination(owner.getReplyCount(), 15);
		List<Reply> replyList = tm.getReplyList(new ReplyCondition().setAccount(owner), pagination.getBeginOffset(), pagination.getPageInfoCount());
		
		model.addAttribute(Owner_Attribute_Name, owner);
		model.addAttribute("pagination", pagination);
		model.addAttribute("replyList", replyList);
		return "center/reply-list";
	}
	
	@RequestMapping(value="/{id}/medal")
	public String medal(@PathVariable int id, Model model){
		Account owner = getAccount(id);
		Account cua = MyContextProvider.getMyContext().getAccount();
		//权限判断
		if(cua.equals(owner)){
			model.addAttribute(Owner_Attribute_Name, owner);
			return "center/medal";
		}
		throw new NoPermissionException();
	}
	
	@RequestMapping("/{id}/point")
	public String point(@PathVariable int id, Model model){
		Account owner = getAccount(id);
		Account cua = MyContextProvider.getMyContext().getAccount();
		//权限判断
		if(cua.equals(owner)){
			model.addAttribute(Owner_Attribute_Name, owner);
			return "center/point";
		}
		throw new NoPermissionException();
	}
	
	@RequestMapping("/{id}/message/inbox")
	public String inbox(@PathVariable int id, Model model){
		Account owner = getAccount(id);
		int total = mm.totalReceived(owner);
		Pagination pagination = new Pagination(total, Message_Count_Per_Page);
		Inbox box = new Inbox(owner, Message_Count_Per_Page, pagination.getBeginOffset());
		mm.fill(box);
		List<Message> messageList = box.getMessages();

		model.addAttribute("pagination", pagination);
		model.addAttribute("messageList", messageList);
		model.addAttribute("type", "inbox");
		model.addAttribute(Owner_Attribute_Name, owner);
		return "center/message-list";
	}
	
	@RequestMapping("/{id}/message/outbox")
	public String outbox(@PathVariable int id, Model model){
		Account owner = getAccount(id);
		int total = mm.totalSent(owner);
		Pagination pagination = new Pagination(total, Message_Count_Per_Page);
		Outbox box = new Outbox(owner, Message_Count_Per_Page, pagination.getBeginOffset());
		mm.fill(box);
		List<Message> messageList = box.getMessages();

		model.addAttribute("pagination", pagination);
		model.addAttribute("messageList", messageList);
		model.addAttribute("type", "outbox");
		model.addAttribute(Owner_Attribute_Name, owner);
		return "center/message-list";
	}
	
	@RequestMapping("/{id}/feed")
	public String feed(@PathVariable int id){
		return "center/feed";
	}
	
}
